package com.yzyz.management.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.yzyz.bean.region.RegionBean;
import com.yzyz.service.RegionService;
import com.yzyz.utils.ValidateUtils;
import com.yzyz.utils.error.ErrorBean;
import com.yzyz.utils.error.Error;

/**
 * 
 * @author David Sun
 * @Date   2010-3-26
 *
 */
public class RegionController extends MultiActionController {
	private RegionService regionService;
	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}

	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		if(ValidateUtils.isNull(request.getParameter("regionId"))){
			ErrorBean error = ErrorBean.getInstance(Error.OBJECT_NOT_EXIST);
			this.writeToClient(response, error.toJson());
			return;
		}
		this.regionService.delete(Integer.valueOf(request.getParameter("regionId")));
		ErrorBean error = ErrorBean.getInstance(Error.SUCCESS, "删除成功");
		this.writeToClient(response, error.toJson());
	}
	
	public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name 		= request.getParameter("name");
		String description 	= request.getParameter("description");
		if(ValidateUtils.isNull(name)){
			ErrorBean error = ErrorBean.getInstance(Error.REGION_NAME_REQUIRED);
			this.writeToClient(response, error.toJson());
			return;
		}
		RegionBean region = new RegionBean();
		region.setName(name);
		region.setDescription(description);
		region.setDate(new Date());
		region.setLevel(Integer.valueOf(request.getParameter("level")));
		int result = this.regionService.add(region);
		if(result != Error.SUCCESS){
			ErrorBean error = ErrorBean.getInstance(result);
			this.writeToClient(response, error.toJson());
			return;
		} 
		ErrorBean error = ErrorBean.getInstance(Error.SUCCESS, "添加成功", region.getId());
		this.writeToClient(response, error.toJson());
	}
	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String name 		= request.getParameter("name");
		String description 	= request.getParameter("description");
		int level 			= Integer.valueOf(request.getParameter("level"));
		if(ValidateUtils.isNull(name)){
			ErrorBean error = ErrorBean.getInstance(Error.REGION_NAME_REQUIRED);
			this.writeToClient(response, error.toJson());
			return;
		}
		RegionBean region = new RegionBean();
		region.setLevel(level);
		region.setName(name);
		region.setDescription(description);
		region.setId(Integer.valueOf(request.getParameter("regionId")));
		this.regionService.update(region);
		ErrorBean error = ErrorBean.getInstance(Error.SUCCESS, "操作成功");
		this.writeToClient(response, error.toJson());
	}
	/**
	 * disable region
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void disable(HttpServletRequest request, HttpServletResponse response) throws IOException{
		if(ValidateUtils.isNull(request.getParameter("regionId"))){
			ErrorBean error = ErrorBean.getInstance(Error.OBJECT_NOT_EXIST);
			this.writeToClient(response, error.toJson());
			return;
		}
		this.regionService.disable(Integer.valueOf(request.getParameter("regionId")));
		ErrorBean error = ErrorBean.getInstance(Error.SUCCESS, "操作成功");
		this.writeToClient(response, error.toJson());
	}
	/**
	 * enable region
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void enable(HttpServletRequest request, HttpServletResponse response) throws IOException {
		if(ValidateUtils.isNull(request.getParameter("regionId"))){
			ErrorBean error = ErrorBean.getInstance(Error.OBJECT_NOT_EXIST);
			this.writeToClient(response, error.toJson());
			return;
		}
		this.regionService.enable(Integer.valueOf(request.getParameter("regionId")));
		ErrorBean error = ErrorBean.getInstance(Error.SUCCESS, "操作成功");
		this.writeToClient(response, error.toJson());
	}
	public void listRegions(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int start = 0;
		int limit = 30;
		if(ValidateUtils.isNull(request.getParameter("start"))){
			start = Integer.valueOf(request.getParameter("start"));
		}
		if(ValidateUtils.isNull(request.getParameter("limit"))){
			limit = Integer.valueOf(request.getParameter("limit"));
		}
		List<RegionBean> regions = this.regionService.listRegions(start, limit);
		JsonConfig config = new JsonConfig();
		String excludes [] = {"scenics","date"};
		config.setExcludes(excludes);
		JSONArray arr = JSONArray.fromObject(regions, config);
		StringBuffer sb 			= new StringBuffer();
		sb.append("{\"sum\":").append("\""+this.regionService.listRegions(start, limit)+"\",");
		sb.append("\"data\":").append(arr.toString());
		sb.append("}");
		this.writeToClient(response, sb.toString());
	}
	
	public void getRegionsForScenic(HttpServletRequest request, HttpServletResponse response) throws IOException{
		List<RegionBean> regions = this.regionService.listAllRegions();
		JsonConfig config = new JsonConfig();
		String excludes [] = {"scenics","date","level","available","description"};
		config.setExcludes(excludes);
		JSONArray arr = JSONArray.fromObject(regions, config);
		this.writeToClient(response, new StringBuilder().append("{\"regions\":").append(arr.toString()).append("}").toString());
	}
	
	private void writeToClient(HttpServletResponse response, String contents) throws IOException {
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Programa", "no-cache");
		response.setDateHeader("Expires", -1L);
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(contents);
		response.flushBuffer();
		response.getWriter().close();
	}
}
